package com.itheima.web.controller;

import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.domain.db.Options;
import com.itheima.domain.db.Questionnaire;
import com.itheima.domain.db.SoulQuestion;
import com.itheima.web.manager.HouTaiSoulManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController()
@RequestMapping("/houtai")
@CrossOrigin("*")
public class HouTaiSoulController {
    @Autowired
    private HouTaiSoulManager houTaiSoulManager;
    @Autowired
    private OssTemplate ossTemplate;

    //分页查询题库
    @GetMapping("/findQuestionBank")
    public ResponseEntity findQuestionBank(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                           String cid){
        return houTaiSoulManager.findQuestionBank(pageNum,pageSize,cid);
    }

    //新增题库
    @PostMapping("/addQuestionBank")
        public String addQuestionBank(@RequestBody Questionnaire questionnaire){
        houTaiSoulManager.addQuestionBank(questionnaire);
        return "ok";
    }

    //删除题库
    @DeleteMapping("/deleteById")
    public String deleteById(Integer id){
        houTaiSoulManager.deleteById(id);
        return "ok";
    }

    //图片上传
    @PostMapping("/file")
    public String upload(MultipartFile uploadFile) throws IOException {
        String picUrl = ossTemplate.upload(uploadFile.getOriginalFilename(), uploadFile.getInputStream());
        return picUrl;
    }

    //查询题库
    @GetMapping("/findQuestion")
    public ResponseEntity findQuestion(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       String bankId){
        return houTaiSoulManager.findQuestion(pageNum,pageSize,bankId);
    }

    //添加题目
    @PostMapping("/addQuestion")
    public String addQuestion(@RequestBody SoulQuestion soulQuestion){
        houTaiSoulManager.addQuestion(soulQuestion);
        return "ok";
    }

    //根据id删题
    @DeleteMapping("/deleteQuestionById")
    public String deleteQuestionById(Integer id){
        houTaiSoulManager.deleteQuestionById(id);
        return "ok";
    }

    //批量删题
    @DeleteMapping("/deleteMoreQuestion")
    public String deleteMoreQuestion(Integer[] ids){
        houTaiSoulManager.deleteMoreQuestion(ids);
        return "ok";
    }

    //查询答案
    @GetMapping("/findSolution")
    public ResponseEntity findSolution(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       Integer bankId){
        return houTaiSoulManager.findSolution(pageNum,pageSize,bankId);
    }

    //新增答案
    @PostMapping("/saveSolution")
    public String saveSolution(@RequestBody Options options){
        houTaiSoulManager.saveSolution(options);
        return "ok";
    }

    //删除指定选项
    @DeleteMapping("/deleteSolutionById")
    public String deleteSolutionById(Integer id){
        houTaiSoulManager.deleteSolutionById(id);
        return "ok";
    }

    //删除多个选项
    @DeleteMapping("/deleteMoreSolution")
    public String deleteMoreSolution(Integer[] ids){
        houTaiSoulManager.deleteMoreSolution(ids);
        return "ok";
    }
}
